from PyQt5.QtWidgets import QMainWindow, QWidget, QVBoxLayout, QLabel, QPushButton
from PyQt5.QtCore import Qt
from crawler.huxiu_crawler import HuxiuCrawler

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("微信AI助手")
        self.setMinimumSize(800, 600)
        
        # 初始化爬虫
        self.crawler = HuxiuCrawler()
        
        # 主布局
        main_widget = QWidget()
        self.setCentralWidget(main_widget)
        
        layout = QVBoxLayout()
        main_widget.setLayout(layout)
        
        # 标题
        title_label = QLabel("微信AI助手")
        title_label.setAlignment(Qt.AlignCenter)
        title_label.setStyleSheet("font-size: 24px; font-weight: bold;")
        layout.addWidget(title_label)
        
        # 功能按钮
        self.start_button = QPushButton("开始")
        self.start_button.setFixedHeight(40)
        layout.addWidget(self.start_button)
        
        self.stop_button = QPushButton("停止")
        self.stop_button.setFixedHeight(40)
        layout.addWidget(self.stop_button)
        
        # 状态显示
        self.status_label = QLabel("状态：未启动")
        self.status_label.setAlignment(Qt.AlignCenter)
        layout.addWidget(self.status_label)
        
        # 连接信号槽
        self.start_button.clicked.connect(self.on_start)
        self.stop_button.clicked.connect(self.on_stop)
        
    def on_start(self):
        self.status_label.setText("状态：运行中")
        try:
            print("开始爬取数据...")
            articles = self.crawler.crawl()
            print(f"成功爬取{len(articles)}篇文章")
            self.status_label.setText(f"状态：已爬取{len(articles)}篇文章")
        except Exception as e:
            print(f"爬取失败: {str(e)}")
            self.status_label.setText("状态：爬取失败")
        
    def on_stop(self):
        self.status_label.setText("状态：已停止")
